
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <pthread.h>
#include <string.h>
#include <error.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <math.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/epoll.h>
#include <pthread.h>



#ifdef _D
#define DBG(fmt, args...) printf(fmt, ##args)
#else
#define DBG(fmt, args...) 
#endif


int main(){
    char* ip = "127.0.0.1";
    struct sockaddr_in addr;
    bzero(&addr, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(12345);
    if(inet_aton(ip, &(addr.sin_addr)) == -1){
        return -1;
    }
    int fd = socket(AF_INET, SOCK_STREAM, 0);
    connect(fd, (struct sockaddr*)&addr, sizeof(addr));

    struct sockaddr_in host;
    struct sockaddr_in peer;
    bzero(&host, sizeof(host));
    bzero(&peer, sizeof(peer));
    socklen_t len1 = sizeof(host);
    socklen_t len2 = sizeof(peer);

    getsockname(fd, (struct sockaddr*)&host, &len1);
    getpeername(fd, (struct sockaddr*)&peer, &len2);
    printf("host is address %s and port %d\n", inet_ntoa(host.sin_addr), ntohs(host.sin_port));
    printf("peer is address %s and port %d\n", inet_ntoa(peer.sin_addr), ntohs(peer.sin_port));

}